import axios from 'axios'
import host from './host'
import history from "../history";
import { getToken, setToken, getJsonFromUrl } from '../util/util'

var params = getJsonFromUrl()
// login with token
if (process.env.NODE_ENV !== 'production') {
    var token = params.__token
    if (token) {
        setToken(token)
    }
}


// initialize axios
axios.defaults.baseURL = host.BASE_URL
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

axios.interceptors.request.use((config) => {
    config.headers.common.Authorization = config.headers.common.Authorization ? config.headers.common.Authorization : getToken()
    return config
})

// axios.interceptors.response.use(undefined, (error) => {
//     if (error.response) {
//         switch (error.response.status) {
//             case 401:
//                 if (params.__ticket) return
//                 // wx mp login
//                 const _l = history.location;
//                 const next = _l.pathname + _l.search

//                 const url = window.location.origin + next
//                 window.location.href = '/account2/login/?business_url=' + encodeURIComponent(url)
//                 return

//             default:
//                 break
//         }
//     }
//     return Promise.reject(error)
// })
